package com.general.framework.core.exception;

import java.util.Objects;

/**
 * @author leejean
 * BaseException
 * 异常处理父类，所有的异常处理基于该类派生
 * @date 2020年05月20 10:17:49
 */
public class BaseException extends RuntimeException {

    private static final long serialVersionUID = -1535405572500029548L;

    /**
     * 错误代码
     */
    protected Integer code;

    public BaseException(final Throwable cause) {
        super(cause);
        this.code = getDefaultCode();
    }

    public BaseException(final ICode errorCode) {
        this(errorCode.getCode(), errorCode.getMessage());
    }

    public BaseException(final String message, final Throwable cause) {
        super(message, cause);
        this.code = getDefaultCode();
    }

    public BaseException(final String message) {
        super(message);
        this.code = getDefaultCode();
    }

    public BaseException(final Integer code, final String message) {
        this(message);
        this.code = Objects.isNull(code) ? getDefaultCode() : code;
    }

    public BaseException(final Integer code, final String message, final Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public String getErrorDetail() {
        return Ex.getStackTraceAsString(this);
    }

    public Integer getDefaultCode() {
        return CodeEnum.SYSTEM_ERROR.getCode();
    }
}
